package com.scada.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scada.dao.IserviceInfoDao;
import com.scada.inf.ServiceInfoVisitor;
import com.scada.model.bean.ServiceInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.ServiceInfoSearch;

@Service("serviceInfoVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class ServiceInfoVisitorImpl implements ServiceInfoVisitor{

	@Autowired
	private IserviceInfoDao serviceInfoDao;
	
	@Transactional
	@Override
	public void deleteServiceInfoById(String isShow, String ids) {
		
		try {
			Map<String,Object> map = null;
			for(String id : ids.split(",")) {
				map = new HashMap<String,Object>(); 
				map.put("isShow", isShow);
				map.put("id", Long.parseLong(id));
				serviceInfoDao.deleteServiceInfoById(map);
			}
		}catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@Override
	public ResponseBean insertServiceInfo(ServiceInfo serviceInfo) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			serviceInfoDao.insertServiceInfo(serviceInfo);
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getServiceInfoById(Long id) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			responseBean.addObject("result", serviceInfoDao.getServiceInfoById(id));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean updateServiceInfo(ServiceInfo serviceInfo) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			serviceInfoDao.updateServiceInfo(serviceInfo);
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getServiceInfoPageData(ServiceInfoSearch serviceInfoSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			map.put("isShow", ServiceInfo.IsShowCode.YES.getKey());
			map.put("serviceType", serviceInfoSearch.getServiceType());
			map.put("serviceName", serviceInfoSearch.getServiceName());
			map.put("investmentType", serviceInfoSearch.getInvestmentType());
			responseBean.addObject("resultCount", serviceInfoDao.getServiceInfoPageCount(map));
			map.put("start", serviceInfoSearch.getStart());
			map.put("pageSize", serviceInfoSearch.getLength());
            responseBean.addObject("resultList", serviceInfoDao.getServiceInfoPageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean repeatNameValidate(Long id, String serviceName, String serviceType) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			map.put("id", id);
			map.put("serviceName", serviceName);
			map.put("serviceType", serviceType);
			responseBean.addObject("result", serviceInfoDao.repeatNameValidate(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getSelectService(String pageSize, String currentPage, String searchName, String serviceType) {
		
		ResponseBean rb=new ResponseBean();
		try{
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("searchName", searchName);
			map.put("serviceType", serviceType);
			map.put("isShow", ServiceInfo.IsShowCode.YES.getKey());
			rb.addObject("resultCount", serviceInfoDao.getSelectServicePageCount(map));
			map.put("start", Integer.parseInt(currentPage)*Integer.parseInt(pageSize));
			map.put("pageSize", Integer.parseInt(pageSize));
			rb.addObject("resultList", serviceInfoDao.getSelectServicePageList(map));
			rb.success();
		}catch(Exception e){
			rb.failed(e.getMessage());
		}
		return rb;
	}

}
